package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/11/4 10:01
 * @question:
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 可以使用此注解观察缓存情况
     */
    @Autowired
    private CacheManager cacheManager;

    /**
     * 添加套餐 setmealDish表
     * 请求 URL: http://localhost:8080/setmeal
     * 请求方法: POST
     * @param setmealDto
     * @return
     */
    @PostMapping
//    @CacheEvict(value = "setmealCache",allEntries = true)
    @CacheEvict(value = "setmealCache",key = "#setmealDto.categoryId + '_' + #setmealDto.status")
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveWithDish(setmealDto);

        return R.success("添加套餐成功~");
    }

    /**
     * 分页条件查询
     * 请求 URL: http://localhost:8080/setmeal/page?page=1&pageSize=10
     * 请求方法: GET
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String name) {

        Page<SetmealDto> setmealDtoPage = setmealService.pageR(page, pageSize, name);

        return R.success(setmealDtoPage);
    }

    /**
     * 套餐的删除/批量删除
     * 删除套餐:1.售卖状态 2.关系表数据也要删除
     * 请求 URL: http://localhost:8080/setmeal?ids=1588394342638223362
     * 请求方法: DELETE
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> removeR(@RequestParam List<Long> ids) {

        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 修改套餐 -- 回显
     * 请求 URL: http://localhost:8080/setmeal/1587774256797712386
     * 请求方法: GET
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getR(@PathVariable Long id) {

        SetmealDto setmealDto = setmealService.getByWithDish(id);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐数据
     * 请求 URL: http://localhost:8080/setmeal
     * 请求方法: PUT
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateR(@RequestBody SetmealDto setmealDto) {

        setmealService.updateByWithDish(setmealDto);

        return R.success("修改套餐数据成功");
    }

    /**
     * 批量起售停售-- TODO 实战内容
     *
     * 此判断逻辑较为复杂 --> 停售套餐随便停售
     *                     若想起售套餐,则必须满足菜品状态为1
     *
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> status(@RequestParam List<Long> ids, @PathVariable String status) {

        /**
         * 套餐的话理论上是可以直接停售的
         * 他停售了菜品想停售才可以停售
         * 但是套餐想要起售的话
         * 菜品必须是起售状态
         */
        //1.判断套餐状态是否为1 --> 继续往下走
        if("1".equals(status)){
            //2.判断 套餐/菜品 状态是否为售卖状态为0 则可以进行停售操作
            //  必须要想办法获得该套餐id才能进行判断
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper();
            lqw.in(SetmealDish::getSetmealId,ids);
            //调用service成集合对象
            List<SetmealDish> setmealDishList = setmealDishService.list(lqw);

            //必须要判断 菜品/套餐 确定有关联的数据 才能往下走 否则直接跳出
            //如果获取到的id没有加入套餐的话 往里边走会直接报错
            if (setmealDishList.size() != 0) {
                //想要转换成dish对象
                ArrayList<Long> dishIds = new ArrayList<>();
                //遍历 setmealDishList 让其dish的id 全部装进 dish对象
                for (SetmealDish setmealDish : setmealDishList) {
                    dishIds.add(setmealDish.getDishId());
                }

                //调用 dish 条件查询器
                LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper();
                //查询所有ids
                lqw1.in(Dish::getId,dishIds);
                //等值查询条件为0的对象
                lqw1.eq(Dish::getStatus,0);
                //调用service
                int count = dishService.count(lqw1);

                if (count>0){
                    throw new CustomException("不能起售，套餐中存在已禁用的菜品");
                }
            }
        }

        //3.前二个条件都满足了 可以执行 停售/起售 操作
        List<Setmeal> list = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            //停售/起售 都可以操作
            setmeal.setStatus(Integer.parseInt(status));
            list.add(setmeal);
        }
        setmealService.updateBatchById(list);
        return R.success("操作成功");
    }


    /**
     * 在分类中根据套餐分类下拉列表和菜品列表 展示菜品
     * 请求 URL: http://localhost:8080/setmeal/list?categoryId=1397844263642378242
     * 请求方法: GET
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#dishDto.categoryId + '_' + #dishDto.status")
    public R<List<Setmeal>> listR(Setmeal setmeal) {

        //调用service查询 集合
        List<Setmeal> setmeals = setmealService.listR(setmeal);

        return R.success(setmeals);
    }




}
